<?php
namespace app\home\controller;
use think\Controller;
use think\Session;
use think\Db;
use app\home\model\CartModel;

class Cart extends Controller{

    public function goodsInfo(){
        $id=input('id',0);
        $cart=new CartModel();
        $rows=$cart->goodsInfo($id);
        $goodsInfoImages=$cart->goodsInfoImages($id);
        $goodsInfoAppraises=$cart->goodsInfoAppraises($id);
        //购物车计数
        if( Session::get('USER.id') >0 ){
            $count=$cart->count();
            if($count>0){
                $this->assign('count',$count);
            }else{
                $this->assign('count',0);
            }
        }else{
            $this->assign('count',0);
        }
        //对选过的商品数量设置
        if( Session::get('USER.id') >0 ){
            $findCartInfo=$cart->findCartInfo($id);
            if($findCartInfo){
                $this->assign('findCartInfo',$findCartInfo['goods_num']);
            }else{
                $this->assign('findCartInfo',1);
            }
        }else{
            $this->assign('findCartInfo',1);
        }
        $this->assign('data',$rows);
        $this->assign('goodsInfoImages',$goodsInfoImages);
        $this->assign('goodsInfoAppraises',$goodsInfoAppraises);
        return $this->fetch('goods_info');
    }
    public function addCart(){
        $goodsId=input('goodsId',0);
        $goods_amount=input('shopPrice',0);
        $goods_num=input('num',0);
        if( Session::get('USER.id') >0 ){
        $addCart=new CartModel();
        $res=$addCart->addCart($goodsId,$goods_amount,$goods_num);
        print_r($res);
        }else{
            echo  -2;
        }
    }
    public function seeCart(){
        if( Session::get('USER.id') >0 ){
            echo 1;
        }else{
            echo  -1;
        }
    }
    //购物车列表
    public function cartList(){
            $cart=new CartModel();
            $rows=$cart->cartList();
            $this->assign('rows',$rows);
            return $this->fetch('cart_list');
    }

    //减少数量
    public function reduceCart(){
        $goodsId=input('goodsId',0);
        $goods_num=input('num',0);
        $addCart=new CartModel();
        $res=$addCart->reduceCart($goodsId,$goods_num);
        if($res>0){
            echo 1;
        }else{
            echo -1;
        }
    }
    //增加数量
    public function increaseNum(){
        $goodsId=input('goodsId',0);
        $goods_num=input('num',0);
        $addCart=new CartModel();
        $res=$addCart->increaseNum($goodsId,$goods_num);
        if($res>0){
            echo 1;
        }else{
            echo -1;
        }
    }
    //直接改变数量
    public function changeGoodsNum(){
        $goodsId=input('goodsId',0);
        $goods_num=input('num',0);
        $addCart=new CartModel();
        $res=$addCart->changeGoodsNum($goodsId,$goods_num);
        if($res>0){
            echo 1;
        }else{
            echo -1;
        }
    }
    //合计总价用于aiax
    public function goodsTotal(){
        $addCart=new CartModel();
        $rows=$addCart->goodsTotal();
        $total="";
        foreach($rows  as  $v){
            $total=$total + $v['goods_amount']*$v['goods_num'];
        }
        print_r($total);
    }
    //删除购物车商品
    public function deleteGoods(){
        $goodsId=input('goodsId',0);
        $addCart=new CartModel();
        $res=$addCart->deleteGoods($goodsId);
        if($res>0){
            echo 1;
        }else{
            echo -1;
        }
    }
    //判断库存
    public function SettlementByCart(){
        if( Session::get('USER.id') >0 ){
            $cart=new CartModel();
            //先判断当前用户购物车是否有商品提交订单时以免报错
            $searchCart=$cart->searchCart();
            if( $searchCart ){
                $rows=$cart->cartList();
                foreach($rows  as $v){
                    if($v['goods_num'] >$v['goods_stock']  ){
                        print_r(json_encode($v));
                        return;
                    }
                }
            }else{
                echo -2;
            }
        }else{
            echo -1;
        }
    }
    //合计总价用于显示
    public function goodsTotalShow(){
        $addCart=new CartModel();
        $rows=$addCart->goodsTotal();
        $total="";
        foreach($rows  as  $v){
            $total=$total + $v['goods_amount']*$v['goods_num'];
        }
        return  $total  ;
    }
    //结算并确认订单显示页面
    public function confirmOrder(){
        $cart=new CartModel();
        $rows=$cart->cartList();
        $count=$cart->count();
        $goodsTotalShow=$this->goodsTotalShow();
        $userAddress=$cart->userAddress();

//        echo "<pre>";
//        print_r($userAddress);
//        exit;
        $this->assign('userAddress',$userAddress);
        $this->assign('count',$count);
        $this->assign('goodsTotalShow',$goodsTotalShow);
        $this->assign('rows',$rows);
        return $this->fetch('confirm_order');
    }
    //提交订单
    public function submitOrder(){
        if( Session::get('USER.id') >0 ){
            $CartModel=new  CartModel();
            $rows=$CartModel->userAddress();
            if($rows){
                $searchCart=$CartModel->searchCart();
                if($searchCart){
                    //提交订单操作数据库
                    Db::startTrans();
                    $data['user_id']= Session::get('USER.id');
                    $data['order_num']='ON10000'.time();
                    $data['order_amount']=$this->goodsTotalShow();
                    $data['add_time']=time();
                    try{
                        $res = $CartModel->submitOrder($data);
                        $findOrder=$CartModel->searchSubmitOrder();
                        //获取购物车当前用户的商品
                        $getGoods=$CartModel->getGoodsByCart();
                        $add=array();
                        foreach($getGoods  as  $v){
                            $add[]=array('order_id'=>$findOrder['id'],
                                'goods_id'=>$v['goods_id'],
                                'goods_num'=>$v['goods_num'],
                                'goods_amount'=>$v['goods_amount']
                            );
                        }
                        //备份当前用户购物车商品到order_goods表
                        $addOrderGoods=$CartModel->addOrderGoods($add);
                        //物理删除当前用户购物车商品
                        $deleteCart=$CartModel->deleteCart();
                        // 提交事务
                        Db::commit();
                        print_r($findOrder['id']);
                    }catch (\Exception $e) {
                        // 回滚事务
                        Db::rollback();
                        echo -4;
                    }
                }else{
                    echo -3;
                }
            }else{
                echo -2;
            }
        }else{
            echo -1;
        }
    }
    //显示付款页面
    public function payment(){
        $CartModel=new  CartModel();
        $findOrder=$CartModel->searchSubmitOrder();
//        echo "<pre>";
//        print_r(0);
//        exit;
        $this->assign('findOrder',$findOrder);
        return $this->fetch('payment');
    }



}